
package com.the9tcat.deliverycoming.modle;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ParserDeliveryDetail {

    private static String TIME = "time";

    private static String CONTEXT = "context";

    private static String DATA = "data";

    /**
     * Gets the feed.
     * 
     * @param jsonFeed the json feed
     * @return the feed
     * @throws JSONException the jSON exception
     * @throws UnsupportedEncodingException the unsupported encoding exception
     */
    public static ArrayList<DeliveryDetail> getDeliveryDetails(String result)
            throws JSONException,
            UnsupportedEncodingException {
        JSONObject jsonObject = new JSONObject(result);
        String s = DATA;
        ArrayList<DeliveryDetail> deliveryDetails = new ArrayList<DeliveryDetail>();
        if (jsonObject.has(s)) {

            JSONArray jsondatas = jsonObject.getJSONArray(s);
            for (int i = 0; i < jsondatas.length(); i++) {
                deliveryDetails.add(getDeliveryDetail(jsondatas.getJSONObject(i)));
            }

        }

        return deliveryDetails;
    }

    /**
     * Gets the article.
     * 
     * @param jsonArticle the json article
     * @return the article
     * @throws JSONException the jSON exception
     * @throws UnsupportedEncodingException the unsupported encoding exception
     */
    public static DeliveryDetail getDeliveryDetail(JSONObject jsonDeliveryDetail)
            throws JSONException, UnsupportedEncodingException {

        DeliveryDetail data = new DeliveryDetail();
        // transfer the data from JSON to Java Object
        String s = TIME;
        if (jsonDeliveryDetail.has(s)) {
            data.setTime(jsonDeliveryDetail.getString(s));
        }

        s = CONTEXT;
        if (jsonDeliveryDetail.has(s)) {
            data.setStatus(jsonDeliveryDetail.getString(s));
        }

        return data;
    }

}
